package com.kehutong.payment.admin;

import javax.servlet.http.HttpServletRequest;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.util.ReqBody;
import org.coraframework.mvc.util.RequestUtils;
import com.kehutong.common.errcode.Result;
import com.kehutong.common.webchat.XmlUtil;
import com.kehutong.payment.service.WxpayService;

@MvcAction
@ReqMapping("/admin/payment")
public class PaymentController {
	private static Logger logger = LoggerFactory.getLogger(PaymentController.class);

	@Inject
	private WxpayService wxpayService;

	@ReqMapping(value = "/callback", reqBody = ReqBody.TEXT)
	public Object callback(HttpServletRequest req){
		try {
			String text = RequestUtils.readText(req);
			logger.info("=======#########################PaymentController.callback回调请求参数:{}#############################======", text);
			JSONObject jsonObject = XmlUtil.xml2JSON(text);
			logger.info("=======#########################PaymentController.callback回调请json解析:{}#############################======", jsonObject.toJSONString());
			return wxpayService.callback(jsonObject);
		} catch (Exception e){
			logger.error("PaymentController.callback"+e.getMessage(), e);
			return Result.error(0, "系统异常");
		}
	}

	@ReqMapping(value = "/v2/callback", reqBody = ReqBody.TEXT)
	public Object v2Callback(HttpServletRequest req){
		try {
			String text = RequestUtils.readText(req);
			logger.info("=======#########################PaymentController.v2Callback回调请求参数:{}#############################======", text);
			JSONObject jsonObject = XmlUtil.xml2JSON(text);
			logger.info("=======#########################PaymentController.v2Callback回调请json解析:{}#############################======", jsonObject.toJSONString());
			return wxpayService.v2Callback(jsonObject);
		} catch (Exception e){
			logger.error("PaymentController.v2Callback"+e.getMessage(), e);
			return Result.error(0, "系统异常");
		}
	}
}
